package thread;

//  synchronized : 修饰一个静态方法, 锁的 SynchronizedDemo 类的对象
public class demo16_synchronized {
    private static int count = 0;

    public static void add() {
        //  针对类对象加锁
        synchronized (demo16_synchronized.class) {
            count++;
        }
    }

    synchronized public static void add2() {
        count++;
    }

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                add();
            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                add();
            }
        });

        t1.start();
        t2.start();

        t1.join();
        t2.join();

        System.out.println(count);
    }
}
